﻿using System;
using System.ComponentModel;
using System.Resources;

using Microsoft.WowAddonStudio.Project.Import;
using Microsoft.WowAddonStudio.Project.Import.Ace;

namespace Microsoft.WowAddonStudio.Project.Import
{
    /// <summary>
    /// Implements interface via which users can specify import and target locations
    /// for ACE import.
    /// </summary>
    public partial class CustomUriPickerWizardPane : InnerPane
    {
        #region Lifecycle methods

        /// <summary>
        /// Creates instances of <see cref="CustomUriPickerWizardPane"/>.
        /// </summary>
        public CustomUriPickerWizardPane()
        {
            InitializeComponent();

            //Set resources
            var res = new ResourceManager(typeof (ImportWizardResources));
            HeaderText = res.GetString("UriPickerGroupBox");
            sourceLabel.Text = res.GetString("UriImportLabel");

            SetActive += CustomUriPickerWizardPane_SetActive;
        }

        #endregion

        /// <summary>
        /// Handles the SetActive event of the CustomUriPickerWizardPane control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void CustomUriPickerWizardPane_SetActive(object sender, CancelEventArgs e)
        {
            HandleSettingsAndButtons();
        }

        /// <summary>
        /// Handles the settings and buttons.
        /// </summary>
        private void HandleSettingsAndButtons()
        {
            if (ValidSettings())
            {
                SetWizardButtons(WizardButtons.Back | WizardButtons.Next);
                Settings = new AceImportSettings(new Uri(sourceTextBox.Text));
            }
            else
            {
                SetWizardButtons(WizardButtons.Back);
                Settings = null;
            }
        }

        /// <summary>
        /// Valids the settings.
        /// </summary>
        /// <returns></returns>
        private bool ValidSettings()
        {
            bool result = false;

            try
            {
                Uri sourceUri = new Uri(sourceTextBox.Text);
                result = true;
            }
            catch (UriFormatException)
            {
                //Invalid uri...
            }

            return result;
        }

        /// <summary>
        /// Handles the TextChanged event of the sourceTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void sourceTextBox_TextChanged(object sender, EventArgs e)
        {
            HandleSettingsAndButtons();
        }
    }
}